package lion.framework.core.web.request;

import java.util.ArrayList;
import java.util.List;

import lion.dev.lang.MapJ;
import lion.framework.core.bean.BeanFactory;
import lion.framework.core.conf.Config;
import lion.framework.core.conf.ConfigManager;

/**
 * 请求路径过滤器，如登录过滤等
 *
 * <pre>
 * time   2013 2013-4-24 上午11:32:29
 * mail   hl_0074@sina.com
 * </pre>
 *
 * @author lion
 */
public class RequestInterceptorManager {

	private static List<RequestInterceptor> filters = new ArrayList<>();
	private static boolean init = false;

	public static RequestInterceptor getInterceptor(int i) {

		if (!init) {
			init();
		}
		if (filters.isEmpty() || i < 0 || i >= filters.size()) { return null; }

		return filters.get(i);
	}

	public static void addInterceptor(RequestInterceptor interceptor) {

		filters.add(interceptor);
	}

	private synchronized static void init() {

		if (init) { return; }
		init = true;
		Config config = ConfigManager.getConfig("framework");
		List<MapJ> list = config.getList("framework.web.interceptors.interceptor");

		for (MapJ map : list) {
			String clazz = map.getString("class");
			filters.add((RequestInterceptor) BeanFactory.create(clazz));
		}
	}
}
